#define _CRT_SECURE_NO_WARNINGS
#include"list.h"
Listnode* Buylist(SLDataType x)
{
	Listnode* newnode = (Listnode*)malloc(sizeof(Listnode));
	newnode->data = x;
	newnode->next = NULL;
	newnode->pre = NULL;
	return newnode;
}
void ListInit(Listnode** pphead)
{
	*pphead = Buylist(0);
	(*pphead)->next = *pphead;
	(*pphead)->pre = *pphead;
}
void Listpushback(Listnode* phead, SLDataType x)
{
	assert(phead);
	Listnode* tail = phead->pre;
	Listnode* newnode = Buylist(x);
	tail->next = newnode;
	newnode->pre = tail;
	phead->pre = newnode;
	newnode->next = phead;
}
void Listprint(Listnode* phead)
{
	Listnode* cur = phead->next;
	while (cur != phead)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}
void Listpushfront(Listnode* phead, SLDataType x)
{
	assert(phead);
	Listnode* newnode = Buylist(x);
	Listnode* first = phead->next;
	phead->next = newnode;
	newnode->pre = phead;
	newnode->next = first;
	first->pre = newnode;
}
void Listpopfront(Listnode* phead)
{
	Listnode* first = phead->next;
	phead->next = first->next;
	first->next->pre = phead;
	free(first);
	first = NULL;
}
void Listpopback(Listnode* phead)
{
	assert(phead);
	Listnode* tail = phead->pre;
	tail->pre->next = phead;
	phead->pre= tail->pre;
	free(tail);
	tail = NULL;
}
Listnode* Listfind(Listnode* phead,SLDataType x)
{
	assert(phead);
	Listnode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
void ListInsert(Listnode* pos, SLDataType x)
{
	assert(pos);
	Listnode* newnode = Buylist(x);
	Listnode* cur = pos->pre;
	cur->next = newnode;
	newnode->pre = cur;
	newnode->next = pos;
	pos->pre = newnode;
}
void ListErase(Listnode* pos)
{
	assert(pos);
	Listnode* pre = pos->pre;
	pre->next = pos->next;
	pos->next->pre = pre;
	free(pos);
	pos = NULL;
}
void Listdestory(Listnode* phead)
{
	assert(phead);
	Listnode* cur = phead->next;
	while (cur)
	{
		Listnode* next = cur->next;
		free(cur);
		cur = next;
	}
	free(phead);
	phead = NULL;
}